package com.mission.schedule.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.mission.schedule.adapter.RepeatFriendsRiChengAdapter;
import com.mission.schedule.adapter.RepeatTuiJianMoreAdapter;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.RepeatBean;
import com.mission.schedule.bean.repeat607.RepeatRecommendContent;
import com.mission.schedule.bean.repeat607.RepeatRecommendTitle;
import com.mission.schedule.clock.QueryAlarmData;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.entity.CLRepeatTable;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.DayComparator;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.NetUtil.NetWorkState;
import com.mission.schedule.utils.ProgressUtil;
import com.mission.schedule.utils.RepeatDateUtils;
import com.mission.schedule.R;
import com.mission.schedule.utils.SharedPrefUtil;
import com.mission.schedule.utils.YearDateComparator;
import com.mission.schedule.utils.YinLiYearDateComparator;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class RepeatFriendsRiChengActivity extends BaseActivity implements
        OnClickListener {

    @ViewResId(id = R.id.top_ll_left)
    private LinearLayout top_ll_left;
    @ViewResId(id = R.id.myrepeatfriends_title)
    private TextView myrepeatfriends_title;
    @ViewResId(id = R.id.repeatfriends_lv)
    private ListView repeatfriends_lv;

    @ViewResId(id = R.id.top_sc)
    private HorizontalScrollView top_sc;
    @ViewResId(id = R.id.tv1)
    private TextView tv1;
    @ViewResId(id = R.id.tv2)
    private TextView tv2;
    @ViewResId(id = R.id.tv3)
    private TextView tv3;
    @ViewResId(id = R.id.tv4)
    private TextView tv4;
    @ViewResId(id = R.id.tv5)
    private TextView tv5;
    @ViewResId(id = R.id.tv)
    private TextView tv;

    String name;
    String type;
    RepeatFriendsRiChengAdapter adapter = null;
    Context context;

    List<RepeatRecommendContent.PageBean.ItemsBean> AllList = new ArrayList<RepeatRecommendContent.PageBean.ItemsBean>();
    List<RepeatRecommendContent.PageBean.ItemsBean> everydayList = new ArrayList<RepeatRecommendContent.PageBean.ItemsBean>();
    List<RepeatRecommendContent.PageBean.ItemsBean> workdayList = new ArrayList<RepeatRecommendContent.PageBean.ItemsBean>();
    List<RepeatRecommendContent.PageBean.ItemsBean> everyweekList = new ArrayList<RepeatRecommendContent.PageBean.ItemsBean>();
    List<RepeatRecommendContent.PageBean.ItemsBean> everymonthList = new ArrayList<RepeatRecommendContent.PageBean.ItemsBean>();
    List<RepeatRecommendContent.PageBean.ItemsBean> everyyearList = new ArrayList<RepeatRecommendContent.PageBean.ItemsBean>();
    List<RepeatRecommendContent.PageBean.ItemsBean> nongliList = new ArrayList<RepeatRecommendContent.PageBean.ItemsBean>();
    List<RepeatRecommendContent.PageBean.ItemsBean> backList = new ArrayList<RepeatRecommendContent.PageBean.ItemsBean>();

    ProgressUtil progressUtil = new ProgressUtil();

    @Override
    protected void setListener() {
        top_ll_left.setOnClickListener(this);
        tv.setOnClickListener(this);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        tv5.setOnClickListener(this);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    alterImportantDialog(2, msg.arg1);
                    break;
                case 2:
                    alterImportantDialog(1, msg.arg1);
                    break;
            }
        }
    };

    private void alterImportantDialog(final int types, final int position) {
        final Dialog dialog = new Dialog(context, R.style.dialog_translucent);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        window.setAttributes(params);// 设置生效

        LayoutInflater fac = LayoutInflater.from(context);
        View more_pop_menu = fac.inflate(R.layout.dialog_repeatcommend, null);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(more_pop_menu);
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = getWindowManager().getDefaultDisplay().getWidth();
        dialog.show();


        TextView one = (TextView) more_pop_menu.findViewById(R.id.old_tv);
        TextView dele = (TextView) more_pop_menu.findViewById(R.id.dele_tv);
        if (types == 1) {
            one.setText("推荐到分类");
            dele.setVisibility(View.VISIBLE);
        } else {
            one.setText("添加到我的重复日程");
            dele.setVisibility(View.GONE);
        }

        one.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (types == 1) {
                    alterTypeDialog(position);
                    dialog.dismiss();
                } else {
                    addFenleiNum(position);
                    addRepeat(position);
                    dialog.dismiss();
                }
            }
        });
        dele.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                delete(position);
                dialog.dismiss();
            }
        });
        more_pop_menu.findViewById(R.id.canel_tv).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    private void alterTypeDialog(final int i) {
        final Dialog dialog = new Dialog(context, R.style.dialog_translucent);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        window.setAttributes(params);// 设置生效

        LayoutInflater fac = LayoutInflater.from(context);
        View more_pop_menu = fac.inflate(R.layout.dialog_repeatcommend_type, null);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(more_pop_menu);
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = getWindowManager().getDefaultDisplay().getWidth();
        dialog.show();

        ListView listview = (ListView) more_pop_menu.findViewById(R.id.listview);
        RepeatTuiJianMoreAdapter adapter = new RepeatTuiJianMoreAdapter(context, list);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                addFenlei(position);
                dialog.dismiss();
            }
        });
    }

    private void addRepeat(int i) {
        App app = App.getDBcApplication();
        RepeatBean bean;
        if (repType == -1) {
            if (AllList.get(i).repType == 1) {
                bean = RepeatDateUtils
                        .saveCalendar(
                                AllList.get(i).repTime,
                                1, "", "");
            } else if (AllList.get(i).repType == 2) {
                bean = RepeatDateUtils.saveCalendar(
                        AllList.get(i).repTime,
                        2,
                        AllList.get(i).repTypeParameter
                                .replace("\"", "")
                                .replace("\\", "")
                                .replace("[", "")
                                .replace("]", ""), "");
            } else if (AllList.get(i).repType == 3) {
                bean = RepeatDateUtils.saveCalendar(
                        AllList.get(i).repTime,
                        3,
                        AllList.get(i).repTypeParameter
                                .replace("\"", "")
                                .replace("\\", "")
                                .replace("[", "")
                                .replace("]", ""), "");
            } else if (AllList.get(i).repType == 4) {
                bean = RepeatDateUtils.saveCalendar(
                        AllList.get(i).repTime,
                        4,
                        AllList.get(i).repTypeParameter
                                .replace("\"", "")
                                .replace("\\", "")
                                .replace("[", "")
                                .replace("]", ""), "0");
            } else if (AllList.get(i).repType == 6) {

                bean = RepeatDateUtils.saveCalendar(
                        AllList.get(i).repTime,
                        4,
                        AllList.get(i).repTypeParameter
                                .replace("\"", "")
                                .replace("\\", "")
                                .replace("[", "")
                                .replace("]", ""), "1");
            } else {
                bean = RepeatDateUtils
                        .saveCalendar(
                                AllList.get(i).repTime,
                                5, "", "");
            }
            /**
             * .replace("\"", "")
             .replace("\\", "")
             .replace("[", "")
             .replace("]", "")
             */
            app.insertCLRepeatTableData(0, 0,
                    1, AllList.get(i).repType, 1, 0, 0, 0, 1,
                    AllList.get(i).repTypeParameter, bean.repNextCreatedTime,
                    bean.repLastCreatedTime,
                    DateUtil.formatDateTime(new Date()),
                    bean.repNextCreatedTime,
                    AllList.get(i).repContent,
                    DateUtil.formatDateTime(new Date()), "",
                    "", AllList.get(i).repTime,
                    AllList.get(i).repRingDesc,
                    AllList.get(i).repRingCode,
                    DateUtil.formatDateTime(new Date()), 0, AllList.get(i).recommendName,
                    0, "", "", 0, 0, 0, "", "",
                    0, 0,
                    "", 0, 0,"0","","","","","","");
            app.insertScheduleData(AllList.get(i).repContent,
                    bean.repNextCreatedTime.substring(0, 10),
                    bean.repNextCreatedTime.substring(11, 16),
                    1, 0, 1, 0, 0, 0,
                    0, bean.repNextCreatedTime, "", 0, "", "",
                    app.repschId, bean.repNextCreatedTime,
                    DateUtil.formatDateTime(new Date()), 0, 0,
                    1, AllList.get(i).repRingDesc,
                    AllList.get(i).repRingCode, AllList.get(i).recommendName, 0, 0,
                    0, "", "", 0, 0, 0,0,"","0","0");
        }else {
            if (newAllList.get(i).repType == 1) {
                bean = RepeatDateUtils
                        .saveCalendar(
                                newAllList.get(i).repTime,
                                1, "", "");
            } else if (newAllList.get(i).repType == 2) {
                bean = RepeatDateUtils.saveCalendar(
                        newAllList.get(i).repTime,
                        2,
                        newAllList.get(i).repTypeParameter
                                .replace("\"", "")
                                .replace("\\", "")
                                .replace("[", "")
                                .replace("]", ""), "");
            } else if (newAllList.get(i).repType == 3) {
                bean = RepeatDateUtils.saveCalendar(
                        newAllList.get(i).repTime,
                        3,
                        newAllList.get(i).repTypeParameter
                                .replace("\"", "")
                                .replace("\\", "")
                                .replace("[", "")
                                .replace("]", ""), "");
            } else if (newAllList.get(i).repType == 4) {
                bean = RepeatDateUtils.saveCalendar(
                        newAllList.get(i).repTime,
                        4,
                        newAllList.get(i).repTypeParameter
                                .replace("\"", "")
                                .replace("\\", "")
                                .replace("[", "")
                                .replace("]", ""), "0");
            } else if (newAllList.get(i).repType == 6) {

                bean = RepeatDateUtils.saveCalendar(
                        newAllList.get(i).repTime,
                        4,
                        newAllList.get(i).repTypeParameter
                                .replace("\"", "")
                                .replace("\\", "")
                                .replace("[", "")
                                .replace("]", ""), "1");
            } else {
                bean = RepeatDateUtils
                        .saveCalendar(
                                newAllList.get(i).repTime,
                                5, "", "");
            }
            /**
             * .replace("\"", "")
             .replace("\\", "")
             .replace("[", "")
             .replace("]", "")
             */
            app.insertCLRepeatTableData(0, 0,
                    1, newAllList.get(i).repType, 1, 0, 0, 0, 1,
                    newAllList.get(i).repTypeParameter, bean.repNextCreatedTime,
                    bean.repLastCreatedTime,
                    DateUtil.formatDateTime(new Date()),
                    bean.repNextCreatedTime,
                    newAllList.get(i).repContent,
                    DateUtil.formatDateTime(new Date()), "",
                    "", newAllList.get(i).repTime,
                    newAllList.get(i).repRingDesc,
                    newAllList.get(i).repRingCode,
                    DateUtil.formatDateTime(new Date()), 0, newAllList.get(i).recommendName,
                    0, "", "", 0, 0, 0, "", "",
                    0, 0,
                    "", 0, 0,"0","","","","","","");
            app.insertScheduleData(newAllList.get(i).repContent,
                    bean.repNextCreatedTime.substring(0, 10),
                    bean.repNextCreatedTime.substring(11, 16),
                    1, 0, 1, 0, 0, 0,
                    0, bean.repNextCreatedTime, "", 0, "", "",
                    app.repschId, bean.repNextCreatedTime,
                    DateUtil.formatDateTime(new Date()), 0, 0,
                    1, newAllList.get(i).repRingDesc,
                    newAllList.get(i).repRingCode, newAllList.get(i).recommendName, 0, 0,
                    0, "", "", 0, 0, 0,0,"","0","0");
        }

        app.updateRepeatTimeNextDate(app.repschId + "", bean.repNextCreatedTime);
        QueryAlarmData.writeAlarm(getApplicationContext());

        Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_repeatfriendsricheng);
    }

    SharedPrefUtil sharedPrefUtil;

    @Override
    protected void init(Bundle savedInstanceState) {
        context = this;
        type = getIntent().getStringExtra("type");
        name = getIntent().getStringExtra("name");
        sharedPrefUtil = new SharedPrefUtil(this, ShareFile.USERFILE);
        userid = sharedPrefUtil.getString(this, ShareFile.USERFILE,
                ShareFile.USERID, "");
        username = sharedPrefUtil.getString(this, ShareFile.USERFILE,
                ShareFile.USERNAME, "");
        if (name.equals("最新分享")){
            top_sc.setVisibility(View.VISIBLE);
        }
        myrepeatfriends_title.setText(name);
        View footer = LayoutInflater.from(context).inflate(R.layout.emptry_foolterview, null);
        repeatfriends_lv.addFooterView(footer);
        backList = App.getDBcApplication().getAllRepeatContentData(type);
        list = App.getDBcApplication().getAllRepeatTypeData();
        loadData(backList.size() > 0 ? true : false);

    }

    List<RepeatRecommendTitle.PageBean.ItemsBean> list = new ArrayList<>();

    private void loadData(boolean se) {
        if (NetUtil.getConnectState(context) != NetWorkState.NONE) {
            RepeatFriendsRiChengAsync(se);
        } else {
            if (!se) {
                Toast.makeText(context, "请检查您的网络!", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void setAdapter() {
        setcheck(tv);
        px(backList,-1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_ll_left:
                Intent intent = new Intent();
                setResult(Activity.RESULT_OK, intent);
                this.finish();
                break;

            case R.id.tv:
                setcheck(tv);
                px(backList,-1);
                break;
            case R.id.tv1:
                setcheck(tv1);
                px(backList,1);
                break;
            case R.id.tv2:
                setcheck(tv2);
                px(backList,5);
                break;
            case R.id.tv3:
                setcheck(tv3);
                px(backList,2);
                break;
            case R.id.tv4:
                setcheck(tv4);
                px(backList,3);
                break;
            case R.id.tv5:
                setcheck(tv5);
                px(backList,4);
                break;

            default:
                break;
        }
    }

    private void setcheck(TextView textView){

        tv.setTextColor(Color.BLACK);
        tv1.setTextColor(Color.BLACK);
        tv2.setTextColor(Color.BLACK);
        tv3.setTextColor(Color.BLACK);
        tv4.setTextColor(Color.BLACK);
        tv5.setTextColor(Color.BLACK);
        textView.setTextColor(Color.parseColor("#df7926"));
    }

    String userid, username;

    private void addFenlei(final int position) {
        if (NetUtil.getConnectState(context) != NetWorkState.NONE) {
            progressUtil.ShowProgress(context, true, true, "正在修改...");

            HttpUtils httpUtils = new HttpUtils(10000);
            RequestParams params = new RequestParams();
            if (repType == -1) {
                params.addBodyParameter("data", "{\"recommend\":[{\"repTime\":\"" + AllList.get(position).repTime + "\"," +
                        "\"repTypeParameter\":\"" + AllList.get(position).repTypeParameter.replace("\"", "\\\"") + "\"," +
                        "\"repType\":" + AllList.get(position).repType + "," +
                        "\"repRingDesc\":\"" + AllList.get(position).repRingDesc + "\"," +
                        "\"repRingCode\":\"" + AllList.get(position).repRingCode + "\"," +
                        "\"repUid\":" + userid + "," +
                        "\"repContent\":\"" + AllList.get(position).repContent + "\"," +
                        "\"ctype\":7," +
                        "\"recommendName\":\"" + username + "\"}]}");
            } else {
                params.addBodyParameter("data", "{\"recommend\":[{\"repTime\":\"" + newAllList.get(position).repTime + "\"," +
                        "\"repTypeParameter\":\"" + newAllList.get(position).repTypeParameter.replace("\"", "\\\"") + "\"," +
                        "\"repType\":" + newAllList.get(position).repType + "," +
                        "\"repRingDesc\":\"" + newAllList.get(position).repRingDesc + "\"," +
                        "\"repRingCode\":\"" + newAllList.get(position).repRingCode + "\"," +
                        "\"repUid\":" + userid + "," +
                        "\"repContent\":\"" + newAllList.get(position).repContent + "\"," +
                        "\"ctype\":7," +
                        "\"recommendName\":\"" + username + "\"}]}");
            }
            params.addBodyParameter("type","1");
            httpUtils.send(HttpMethod.POST, URLConstants.推荐重复加入, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    progressUtil.dismiss();
                    addFenleiNum(position);
                    Toast.makeText(context, "修改成功", Toast.LENGTH_SHORT).show();

                }

                @Override
                public void onFailure(HttpException e, String s) {
                    progressUtil.dismiss();
                    Toast.makeText(context, "修改失败", Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            Toast.makeText(context, "请检查您的网络!", Toast.LENGTH_SHORT).show();
        }
    }

    private void addFenleiNum(int position) {
        if (NetUtil.getConnectState(context) != NetWorkState.NONE) {

            HttpUtils httpUtils = new HttpUtils(10000);
            RequestParams params = new RequestParams();
            params.addBodyParameter("type", "" + type);
            if (repType == -1) {
                params.addBodyParameter("cId", "" + AllList.get(position).id);
            }else {
                params.addBodyParameter("cId", "" + newAllList.get(position).id);
            }
            httpUtils.send(HttpMethod.POST, URLConstants.推荐重复加入次数, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {


                }

                @Override
                public void onFailure(HttpException e, String s) {

                }
            });
        }
    }

    private void delete(final int position) {
        if (NetUtil.getConnectState(context) != NetWorkState.NONE) {
            progressUtil.ShowProgress(context, true, true, "正在删除...");
            HttpUtils httpUtils = new HttpUtils(10000);
            RequestParams params = new RequestParams();
            if (repType == -1) {
                params.addBodyParameter("cId", "" + AllList.get(position).id);
            }else {
                params.addBodyParameter("cId", "" + newAllList.get(position).id);
            }
            params.addBodyParameter("type", type.equals("7") ? "0" : "1");
            httpUtils.send(HttpMethod.POST, URLConstants.重复推荐删除, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    progressUtil.dismiss();
                    adapter.remove(position);
                    if (repType == -1) {
                        AllList.remove(position);
                    } else {
                        newAllList.remove(position);
                    }
                    Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    progressUtil.dismiss();
                    Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            Toast.makeText(context, "请检查您的网络!", Toast.LENGTH_SHORT).show();
        }
    }

    private void RepeatFriendsRiChengAsync(final boolean issetadapter) {
        if (!issetadapter) {
            progressUtil.ShowProgress(context, true, true, "正在加载...");
        }
        HttpUtils httpUtils = new HttpUtils(10000);
        RequestParams params = new RequestParams();
        params.addBodyParameter("nowPage", "0");
        params.addBodyParameter("pageNum", "200");
        params.addBodyParameter("type", "" + type);
        httpUtils.send(HttpMethod.POST, URLConstants.重复推荐详情, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                if (!TextUtils.isEmpty(responseInfo.result)) {
                    Gson gson = new Gson();
                    try {
                        RepeatRecommendContent backBean = gson
                                .fromJson(responseInfo.result,
                                        RepeatRecommendContent.class);
                        if (backBean.status == 0) {
                            List<RepeatRecommendContent.PageBean.ItemsBean> lists = backBean.page.items;
                            if (lists != null && lists.size() > 0) {
                                App.getDBcApplication().deleteRepeatCommendTypeAllContentData(lists.get(0).ctype);
                                for (RepeatRecommendContent.PageBean.ItemsBean bean : lists) {
                                    App.getDBcApplication().saveRepeatCommendContentData(bean);
                                }
//                                if (!issetadapter){
                                backList = backBean.page.items;
                                setAdapter();
//                                }
                            }
                        }
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                    }
                }
                if (!issetadapter) {
                    progressUtil.dismiss();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                if (!issetadapter) {
                    progressUtil.dismiss();
                }
            }
        });

    }

    int repType = -1;
    private void px(List<RepeatRecommendContent.PageBean.ItemsBean> backList,int reptype) {
        repType = reptype;
        AllList.clear();
        everydayList.clear();
        workdayList.clear();
        everyweekList.clear();
        everymonthList.clear();
        everyyearList.clear();
        nongliList.clear();

        for (int i = 0; i < backList.size(); i++) {
            if ("1".equals(backList.get(i).repType)) {
                everydayList.add(backList.get(i));
            } else if ("2".equals(backList.get(i).repType)) {
                everyweekList.add(backList.get(i));
                Collections.sort(everyweekList, new Comparator<RepeatRecommendContent.PageBean.ItemsBean>() {
                    @Override
                    public int compare(RepeatRecommendContent.PageBean.ItemsBean lhs, RepeatRecommendContent.PageBean.ItemsBean rhs) {
                        return lhs.createTime.replace('T', ' ').compareTo(rhs.createTime.replace('T', ' '));
                    }
                });
            } else if ("3".equals(backList.get(i).repType)) {
                everymonthList.add(backList.get(i));
                Collections.sort(everymonthList, new Comparator<RepeatRecommendContent.PageBean.ItemsBean>() {
                    @Override
                    public int compare(RepeatRecommendContent.PageBean.ItemsBean lhs, RepeatRecommendContent.PageBean.ItemsBean rhs) {
                        return lhs.createTime.replace('T', ' ').compareTo(rhs.createTime.replace('T', ' '));
                    }
                });
            } else if ("4".equals(backList.get(i).repType)) {
                everyyearList.add(backList.get(i));
                Collections.sort(everyyearList, new Comparator<RepeatRecommendContent.PageBean.ItemsBean>() {
                    @Override
                    public int compare(RepeatRecommendContent.PageBean.ItemsBean lhs, RepeatRecommendContent.PageBean.ItemsBean rhs) {
                        return lhs.createTime.replace('T', ' ').compareTo(rhs.createTime.replace('T', ' '));
                    }
                });
            } else if ("6".equals(backList.get(i).repType)) {
                nongliList.add(backList.get(i));
                Collections.sort(nongliList, new Comparator<RepeatRecommendContent.PageBean.ItemsBean>() {
                    @Override
                    public int compare(RepeatRecommendContent.PageBean.ItemsBean lhs, RepeatRecommendContent.PageBean.ItemsBean rhs) {
                        return lhs.createTime.replace('T', ' ').compareTo(rhs.createTime.replace('T', ' '));
                    }
                });
            } else {
                workdayList.add(backList.get(i));
            }
        }
        AllList.addAll(everydayList);
        AllList.addAll(workdayList);
        AllList.addAll(everyweekList);
        AllList.addAll(everymonthList);
        AllList.addAll(everyyearList);
        AllList.addAll(nongliList);

        if (reptype == -1) {
            adapter = new RepeatFriendsRiChengAdapter(context, AllList, mScreenWidth, handler, (type.equals("7") ? true : false));
        }else {
            newAllList.clear();
            for (RepeatRecommendContent.PageBean.ItemsBean bean : AllList) {
                if (bean.repType == reptype) {
                    newAllList.add(bean);
                }
            }
            adapter = new RepeatFriendsRiChengAdapter(context, newAllList, mScreenWidth, handler, (type.equals("7") ? true : false));
        }

        repeatfriends_lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
    List<RepeatRecommendContent.PageBean.ItemsBean> newAllList = new ArrayList<RepeatRecommendContent.PageBean.ItemsBean>();
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
